package com.tfkj.repository;

import com.tfkj.entity.Test;
import org.apache.ibatis.annotations.*;

import java.util.List;


@Mapper
public interface TestRepository {

    @Select("select * from schooltest.test where name like CONCAT('%', #{name}, '%') ORDER BY id DESC limit ${pageSize} offset ${offset}")
    List<Test> findTestsByKey(@Param("name") String name, @Param("offset") Integer pageNum, @Param("pageSize") Integer pageSize);

    @Select("select * from schooltest.test ORDER BY id DESC limit ${pageSize} offset ${offset}")
    List<Test> findTests(Integer offset, Integer pageSize);

    @Insert("insert into schooltest.test(name, grade, subject, urls, `type`, testNum) values(#{name},#{grade},#{subject},#{urls}, #{type}, #{testNum})")
    Integer insertTest(@Param("name") String name, @Param("grade") String grade, @Param("subject") String subject, @Param("urls") String urls, @Param("type") String type, @Param("testNum") String testNum);
    
    @Update("update schooltest.test set name = #{name}, grade = #{grade}, subject =#{subject} where `id` = #{id}")
    Integer updateTest(@Param("id")Integer id, @Param("name")String name, @Param("grade") String grade, @Param("subject") String subject);

    @Update("update schooltest.test set urls = #{urls} where `id` = ${id}")
    Integer updateTestUrls(@Param("id")Integer id, @Param("urls")String urls);

    @Delete("delete from schooltest.test where id = ${id}")
    void deleteTest(@Param("id") Integer id);

    @Select("select COUNT(*) from schooltest.test where name like CONCAT('%', #{name}, '%')")
    Integer findTestsByKeyTotal(@Param("name") String name);

    @Select("select COUNT(*) from schooltest.test")
    Integer findTestsTotal();

    @Select("select max(id) from schooltest.test")
    Integer findMaxTestId();

    @Insert("insert into schooltest.test(`id`, name, grade, subject, urls, `type`) values(${id}, #{name},#{grade},#{subject},#{urls}, '')")
    Integer insertTestWithId(@Param("id") Integer id, @Param("name") String name, @Param("grade") String grade, @Param("subject") String subject, @Param("urls") String urls);
}
